package cn.edu.sgu.www.mhxysy.service.equipment.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.equipment.EquipmentCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.equipment.EquipmentCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.equipment.EquipmentCategoryPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.equipment.EquipmentCategoryService;
import cn.edu.sgu.www.mhxysy.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class EquipmentCategoryServiceImpl implements EquipmentCategoryService {

	private final UploadUtils uploadUtils;
	private final ImageUpdater imageUpdater;
	private final EquipmentCategoryMapper equipmentCategoryMapper;

	@Autowired
	public EquipmentCategoryServiceImpl(
			UploadUtils uploadUtils,
			ImageUpdater imageUpdater,
			EquipmentCategoryMapper equipmentCategoryMapper) {
		this.uploadUtils = uploadUtils;
		this.imageUpdater = imageUpdater;
		this.equipmentCategoryMapper = equipmentCategoryMapper;
	}

	@Override
	public String upload(MultipartFile file) throws IOException {
		String directory = CustomDirectories.DIRECTORY_JSZB;

		return uploadUtils.upload(file, directory);
	}

	@Override
	public void insert(EquipmentCategoryDTO equipmentCategoryDTO) {
		EquipmentCategory equipmentCategory = toEntity(equipmentCategoryDTO);

		equipmentCategory.setId(null);

		equipmentCategoryMapper.insert(equipmentCategory);
	}

	@Override
	public void updateById(EquipmentCategoryDTO equipmentCategoryDTO) {
		ObjectUtils.requireNonNull(equipmentCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

		EquipmentCategory equipmentCategory = toEntity(equipmentCategoryDTO);

		equipmentCategoryMapper.updateById(equipmentCategory);
	}

	@Override
	public void updateImageById(Integer id, String image) {
		imageUpdater.updateImageById(image, new Consumer<Object>() {
			@Override
			public void accept(Object o) {
				UpdateWrapper<EquipmentCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

				equipmentCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
			}
		});
	}

	@Override
	public List<EquipmentCategory> selectAll() {
		// 只查询60级以上的装备
		QueryWrapper<EquipmentCategory> queryWrapper = new QueryWrapper<>();

		queryWrapper.ge("grade", 60);

		return equipmentCategoryMapper.selectList(queryWrapper);
	}

	@Override
	public EquipmentCategory selectById(Integer id) {
		return equipmentCategoryMapper.selectById(id);
	}

	@Override
	public List<EquipmentCategory> selectList(Integer type) {
		QueryWrapper<EquipmentCategory> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq(type != null, "type", type);

		return equipmentCategoryMapper.selectList(queryWrapper);
	}

	@Override
	public Page<EquipmentCategory> selectByPage(EquipmentCategoryPager pager) {
		QueryWrapper<EquipmentCategory> queryWrapper = new QueryWrapper<>();
		Page<EquipmentCategory> page = Pager.ofPage(pager);

		queryWrapper.eq(
				pager.getType() != null,
				"type", pager.getType()
		);
		queryWrapper.eq(
				pager.getGrade() != null,
				"grade", pager.getGrade()
		);

		// 得到order by语句
		String statement = Pager.getOrderByStatement(pager);
		
		queryWrapper.last(StringUtils.isNotEmpty(statement), statement);

		return equipmentCategoryMapper.selectPage(page, queryWrapper);
	}

	private EquipmentCategory toEntity(EquipmentCategoryDTO equipmentCategoryDTO) {
		Integer id = equipmentCategoryDTO.getId();
		String name = equipmentCategoryDTO.getName();

		QueryWrapper<EquipmentCategory> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq("name", name);
		queryWrapper.ne(id != null, "id", id);

		Long count = equipmentCategoryMapper.selectCount(queryWrapper);

		if (count > 0) {
			throw new GlobalException(ResponseCode.BAD_REQUEST, "装备名称重复！");
		}

		EquipmentCategory equipmentCategory = new EquipmentCategory();

		equipmentCategory.setId(id);
		equipmentCategory.setName(name);
		equipmentCategory.setNote(equipmentCategoryDTO.getNote());
		equipmentCategory.setType(equipmentCategoryDTO.getType());
		equipmentCategory.setGrade(equipmentCategoryDTO.getGrade());

		return equipmentCategory;
	}

}